<?php

class ValidationHelper extends Helper {

    var $helpers = array('Html', 'Js');

    function rules($modelName, $options = array()) {
        if (empty($options) || !is_array($options) || empty($options['formId'])) {
            return false;
        }

        $scriptTags = '';

        if (!class_exists($modelName)) {
            require_once(APP . 'Model/' . $modelName . '.php');
        }

        $model = new $modelName();

        $defaultOptions = array('formId' => false, 'inline' => false);
        $options = array_merge($defaultOptions, $options);

        $formId = '#' . $options['formId'];

        $scriptTags.= "$(document).ready(function() {\n";
        $scriptTags.= "$('" . $formId . "').submit( function() { return validateForm('" . $modelName . "', rules); });\n";
        $scriptTags.= "});\n";

        $js = "var rules = Array(";

        foreach ($model->validate as $field => $validators) {
            $js.= "['" . $field . "', [";

            foreach ($validators as $validator) {
                if (!is_array($validator)) {
                    $validator = array('rule' => $validator);
                }

                $mensaje = $validator['message'];
                if (!isset($mensaje)) {
                    $mensaje = sprintf('%s %s', __('Hubo un problema con el campo', true), Inflector::humanize($field));
                }

                $rule = $validator['rule'];
                if (is_array($rule)) {
                    $rule = $rule[0];
                }

                $js.= "['" . $mensaje . "', '" . $rule . "'],";
            }

            $js = substr($js, 0, strlen($js) - 1);
            $js.= "]],";
        }

        $js = substr($js, 0, strlen($js) - 1);
        $js.= ");";

        $scriptTags.= $js . "\n";

        $this->Html->scriptBlock($scriptTags, array('inline' => false));

        return true;
    }

    function rulesTables($tables) {
        $scriptTags = "var table_rules = Object();\n";

        foreach ($tables as $table) {
            $has_rules = false;
            $modelName = $table['model'];
            $model = new $modelName();
            $fields = array();

            $js = "table_rules." . $modelName . " = [";

            foreach ($table['fields'] as $field) {
                $fields[] = $field['name'];
            }

            foreach ($model->validate as $field => $validators) {
                $has_rules = true;

                if (!in_array($field, $fields)) {
                    continue;
                }

                $js.= "['" . $field . "', [";

                foreach ($validators as $validator) {
                    if (!is_array($validator)) {
                        $validator = array('rule' => $validator);
                    }

                    $mensaje = $validator['message'];
                    if (!isset($mensaje)) {
                        $mensaje = sprintf('%s %s', __('Hubo un problema con el campo', true), Inflector::humanize($field));
                    }

                    $rule = $validator['rule'];
                    if (is_array($rule)) {
                        $rule = $rule[0];
                    }

                    $js.= "['" . $mensaje . "', '" . $rule . "'],";
                }

                $js = substr($js, 0, strlen($js) - 1);
                $js.= "]],";
            }

            if ($has_rules) {
                $js = substr($js, 0, strlen($js) - 1);
            }
            $js.= "];";

            $scriptTags.= $js . "\n";
        }

        $scriptTags = substr($scriptTags, 0, strlen($scriptTags) - 1);

        $this->Html->scriptBlock($scriptTags, array('inline' => false));
        return true;
    }

}
